package com.yuesystem.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yuesystem.common.CustomException;
import com.yuesystem.common.R;
import com.yuesystem.entity.Feedback;
import com.yuesystem.entity.User;
import com.yuesystem.service.FeedbackService;
import com.yuesystem.service.UserService;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;



/**
 * 反馈功能.
 */
@RestController
@RequestMapping("/feedback")
@CrossOrigin
@Slf4j
public class FeedbackController {
  @Autowired
  private UserService userService;
  
  @Autowired
  private FeedbackService feedbackService;
  
  /**
   * 保存反馈到库中.
   */
  @RequestMapping("/save")
  public R<String> saveFeedback(@RequestBody Feedback feedback) {
    if (feedback.getTitle().isEmpty()) {
      throw new CustomException("反馈标题不能为空，请编辑反馈标题后重试！");
    }
    if (feedback.getContent().isEmpty()) {
      throw new CustomException("反馈内容不能为空，请编辑反馈内容后重试！");
    }
    List<User> list = userService.getService();
    List<Long> serv = new ArrayList<>();
    for (int i = 0; i < list.size(); i++) {
      serv.add(list.get(i).getId());
    }
    Random random = new Random();
    int key = random.nextInt(list.size());
    feedback.setServiceId(serv.get(key));
    feedback.setStatus1(0);
    feedback.setStatus2(0);
    feedbackService.save(feedback);
    return R.success("反馈成功");
  }
  
  
  /**
   * 客服获取未处理的反馈.
   *
   * @param page 页面个数
   * @param pageSize 页面大小
   * @return 未处理的所有反馈列表
   */
  @GetMapping("/list")
  public R<Page<Feedback>> getMyFeedback(int page, int pageSize) {
    Page<Feedback> pageInfo = feedbackService.getPage(page, pageSize);
    return R.success(pageInfo);
  }
  
  
  /**
   * 编辑、管理员获取未处理的反馈.
   */
  @RequestMapping("/list1")
  public R<Page<Feedback>> getFeedback1(int page, int pageSize) {
    Page<Feedback> pageInfo = feedbackService.getPage1(page, pageSize);
    return R.success(pageInfo);
  }
  
  /**
   * 客服获取已处理的反馈.
   */
  @RequestMapping("/solveList")
  public R<Page<Feedback>> getSolve(int page, int pageSize) {
    Page<Feedback> pageInfo = feedbackService.getPage2(page, pageSize);
    return R.success(pageInfo);
  }
  
  /**
   * 编辑或者管理员获得已处理的反馈.
   */
  @RequestMapping("/solveList1")
  public R<Page<Feedback>> getSolve1(int page, int pageSize) {
    Page<Feedback> pageInfo = feedbackService.getPage3(page, pageSize);
    return R.success(pageInfo);
  }
  
  /**
   * 客服处理给管理员.
   */
  @GetMapping("/solveToAdmin/{feedbackId}")
  public R<String> initSolve1(@PathVariable Long feedbackId) {
    Feedback feedback = feedbackService.getById(feedbackId);
    List<User> list = userService.getAdmin();
    List<Long> admin = new ArrayList<>();
    for (int i = 0; i < list.size(); i++) {
      admin.add(list.get(i).getId());
    }
    Random random = new Random();
    int key = random.nextInt(list.size());
    feedback.setDealId(admin.get(key));
    feedback.setStatus1(1);
    feedbackService.updateById(feedback);
    return R.success("成功分配给管理员");
  }
  
  /**
   * 客服处理给编辑.
   */
  @GetMapping("/solveToEdit/{feedbackId}")
  public R<String> initSolve2(@PathVariable Long feedbackId) {
    Feedback feedback = feedbackService.getById(feedbackId);
    List<User> list = userService.getEdit();
    List<Long> edit = new ArrayList<>();
    for (int i = 0; i < list.size(); i++) {
      edit.add(list.get(i).getId());
    }
    Random random = new Random();
    int key = random.nextInt(list.size());
    feedback.setDealId(edit.get(key));
    feedback.setStatus1(1);
    feedbackService.updateById(feedback);
    return R.success("成功分配给编辑");
  }
  
  /**
   * 客服自己处理.
   */
  @GetMapping("/solveBySelf/{feedbackId}")
  public R<String> initSolve3(@PathVariable Long feedbackId) {
    Feedback feedback = feedbackService.getById(feedbackId);
    Long userId = feedback.getServiceId();
    feedback.setDealId(userId);
    feedback.setStatus1(1);
    feedback.setStatus2(1);
    feedbackService.updateById(feedback);
    return R.success("成功处理反馈");
  }
  
  /**
   * 编辑或者管理员处理反馈.
   */
  @GetMapping("/solve/{feedbackId}")
  public R<String> solve(@PathVariable Long feedbackId) {
    Feedback feedback = feedbackService.getById(feedbackId);
    feedback.setStatus2(1);
    feedbackService.updateById(feedback);
    return R.success("成功处理反馈");
  }
  
  /**
   * 展示一条反馈的所有内容.
   *
   * @param feedbackId 反馈的id
   * @return 获取反馈主体的操作结果
   */
  @GetMapping("/feedbackOneList/{feedbackId}")
  public R<Feedback> getOne(@PathVariable Long feedbackId) {
    Feedback feedback = feedbackService.getById(feedbackId);
    return R.success(feedback);
  }
  
  /**
   * 获取用户已经被处理的反馈.
   */
  @GetMapping("/myfinish")
  public R<Page<Feedback>> getPageMy(int page, int pageSize) {
    Page<Feedback> pageInfo = feedbackService.getPageFinish(page, pageSize);
    return R.success(pageInfo);
  }
  
  
}
